package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KKDWebActivity;
import com.kkd.kkdapp.activity.KkdPdfWebActivity;
import com.kkd.kkdapp.http.entity.MyMatchingRecordDataItem;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.view.shapeimageview.PorterShapeImageView;

public class MyMatchingRecordAdapter extends BaseAdapter {
	private Context mContext;
	private List<MyMatchingRecordDataItem> mList;
	private LayoutInflater inflater;
	private DecimalFormat df = new DecimalFormat("###,##0.01");

	public MyMatchingRecordAdapter(Context context,
			List<MyMatchingRecordDataItem> list) {
		this.mContext = context;
		this.mList = list;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(
					R.layout.item_record_mymatching_listview, parent, false);
			holder.top = convertView.findViewById(R.id.top);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.type_bg = (PorterShapeImageView) convertView
					.findViewById(R.id.type_bg);
			holder.type_tv = (TextView) convertView.findViewById(R.id.type_tv);
			holder.toDetail = convertView.findViewById(R.id.toDetail);
			holder.tv_use = (TextView) convertView.findViewById(R.id.tv_use);
			holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
			holder.agreement = (TextView) convertView
					.findViewById(R.id.agreement);
			holder.bottom = convertView.findViewById(R.id.bottom);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (position == 0) {
			holder.top.setVisibility(View.VISIBLE);
		} else {
			holder.top.setVisibility(View.GONE);
		}
		if (position == mList.size() - 1) {
			holder.bottom.setVisibility(View.GONE);
		} else {
			holder.bottom.setVisibility(View.VISIBLE);
		}
		holder.tv_name.setText(mList.get(position).getName());
		holder.tv_use.setText(mList.get(position).getDebtPurpose());
		holder.tv_num.setText("借款总额："
				+ df.format(mList.get(position).getLoanMoney() * 0.01) + "元");
		holder.agreement.setText("《" + mList.get(position).getContractName()
				+ "》");
		holder.agreement.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				String agreeloanUrl = mList.get(position)
						.getContractsDetailUrl();
				if (!TextUtils.isEmpty(agreeloanUrl)) {
					String docUrl = mList.get(position).getContractUrl();
					Intent intent = new Intent(mContext,
							KkdPdfWebActivity.class);
					intent.putExtra("url", agreeloanUrl);
					intent.putExtra("pdfUrl", docUrl);
					intent.putExtra("fileName", "【KKD】"
							+ mList.get(position).getContractName() + "_"
							+ mList.get(position).getLoadId() + ".pdf");
					mContext.startActivity(intent);
				}
			}
		});
		// holder.type_tv.setText(mList.get(position).getGuaranteeType());
		// holder.type_bg.setImageColor(Color.parseColor("#cfcfcf"));
		// LayoutParams params = holder.type_bg.getLayoutParams();
		// params.width = SystemUtils.dip2px(mContext, 11 * mList.get(position)
		// .getGuaranteeType().length() + 12);
		// holder.type_bg.setLayoutParams(params);
		// 跳转到债权匹配详情h5页面
		final String toDetailUrl = mList.get(position).getCreditorDetailUrl();
		holder.toDetail.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (!TextUtils.isEmpty(toDetailUrl)) {
					Intent intent = new Intent(mContext, KKDWebActivity.class);
					intent.putExtra("url", toDetailUrl);
					mContext.startActivity(intent);
				}
			}
		});
		return convertView;
	}

	class ViewHolder {
		View top;
		TextView tv_name;
		PorterShapeImageView type_bg;
		TextView type_tv;
		View toDetail;
		TextView tv_use;
		TextView tv_num;
		TextView agreement;
		View bottom;
	}
}
